/*
  tempgraph: A utility for plotting historical climatology graphs
  Copyright (C) 2009-2013 Bob Mottram
  bob@sluggish.dyndns.org

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "dialogdata.h"
#include "tempgraph.h"
#include "ui_dialogdata.h"
#include <QFileDialog>

DialogData::DialogData(QWidget *parent, GlobalData * globals) :
    QDialog(parent),
    ui(new Ui::DialogData)
{
    ui->setupUi(this);

    this->globals = globals;
    ui->lineEditTemperatureData->clear();
    ui->lineEditWeatherStations->clear();
    ui->lineEditCountryCodes->clear();

    ui->lineEditTemperatureData->setFocus();

    if (TempGraph::fileExists(globals->ghcnDataFilename)) {
        ui->lineEditTemperatureData->setText(globals->ghcnDataFilename);
        ui->lineEditWeatherStations->setFocus();
    }
    if (TempGraph::fileExists(globals->ghcnStationsFilename)) {
        ui->lineEditWeatherStations->setText(globals->ghcnStationsFilename);
        ui->lineEditCountryCodes->setFocus();
    }
    if (TempGraph::fileExists(globals->ghcnCountryCodesFilename)) {
        ui->lineEditCountryCodes->setText(globals->ghcnCountryCodesFilename);
    }

    connect(ui->pushButtonData, SIGNAL(clicked()),
            this, SLOT(openData()));
    connect(ui->pushButtonStations, SIGNAL(clicked()),
            this, SLOT(openStations()));
    connect(ui->pushButtonCountryCodes, SIGNAL(clicked()),
            this, SLOT(openCountryCodes()));
}

DialogData::~DialogData()
{
    delete ui;
}

void DialogData::openData()
{
    ui->lineEditTemperatureData->setText(
        QFileDialog::getOpenFileName(this,
            tr("Open GHCN Temperature Data File"),
            QDir::currentPath(),
            tr("Data Files (*.dat *.txt *.mean)")));

    if (!TempGraph::fileExists(ui->lineEditTemperatureData->displayText())) {
        ui->lineEditTemperatureData->setText("");
    }
}

void DialogData::openStations()
{
    ui->lineEditWeatherStations->setText(
        QFileDialog::getOpenFileName(this,
            tr("Open GHCN Weather Station Data File"),
            QDir::currentPath(),
            tr("Weather Station Files (*.inv *.txt)")));

    if (!TempGraph::fileExists(ui->lineEditWeatherStations->displayText())) {
        ui->lineEditWeatherStations->setText("");
    }
}

void DialogData::openCountryCodes()
{
    ui->lineEditCountryCodes->setText(
        QFileDialog::getOpenFileName(this,
            tr("Open GHCN Country Codes File"),
            QDir::currentPath(),
            tr("Country Codes Files (*.dat *.txt *.codes country-codes)")));

    if (!TempGraph::fileExists(ui->lineEditCountryCodes->displayText())) {
        ui->lineEditCountryCodes->setText("");
    }
}

// returns true if all the fields have valid contents
bool DialogData::fieldsAreValid()
{
    if (ui->lineEditTemperatureData->displayText().length() == 0) {
        // No temperature data file was specified
        ui->lineEditTemperatureData->setFocus();
        return false;
    }
    else {
        if (!TempGraph::fileExists(ui->lineEditTemperatureData->displayText())) {
            // The temperature data file does not exist
            ui->lineEditTemperatureData->setFocus();
            return false;
        }
    }

    if (ui->lineEditWeatherStations->displayText().length() == 0) {
        // no weather station file was specified
        ui->lineEditWeatherStations->setFocus();
        return false;
    }
    else {
        if (!TempGraph::fileExists(ui->lineEditWeatherStations->displayText())) {
            // the weather stations file does not exist
            ui->lineEditWeatherStations->setFocus();
            return false;
        }
    }

    if (ui->lineEditCountryCodes->displayText().length() == 0) {
        // no country codes file was specified
        ui->lineEditCountryCodes->setFocus();
        return false;
    }
    else {
        if (!TempGraph::fileExists(ui->lineEditCountryCodes->displayText())) {
            // the country codes file does not exist
            ui->lineEditCountryCodes->setFocus();
            return false;
        }
    }
    return true;
}

// copy data from the specified directories into the given directory
int DialogData::copyData()
{
    QString command;
    int retval=0;

    // copy the temperature data
    if (ui->lineEditTemperatureData->displayText() != globals->dataDirectory) {
        command = QString(COPY_FILE) + " " + ui->lineEditTemperatureData->displayText() + " " + globals->ghcnDataFilename;
        retval = system(command.toStdString().c_str());
    }

    // copy the weather station data
    if (ui->lineEditWeatherStations->displayText() != globals->ghcnStationsFilename) {
        command = QString(COPY_FILE) + " " + ui->lineEditWeatherStations->displayText() + " " + globals->ghcnStationsFilename;
        retval = system(command.toStdString().c_str());
    }

    // copy the country codes
    if (ui->lineEditCountryCodes->displayText() != globals->ghcnCountryCodesFilename) {
        command = QString(COPY_FILE) + " " + ui->lineEditCountryCodes->displayText() + " " + globals->ghcnCountryCodesFilename;
        retval = system(command.toStdString().c_str());
    }

    return retval;
}

// Ok button was pressed
void DialogData::accept()
{
    if (fieldsAreValid()) {
        copyData();
        globals->validData=true;

        this->close();
    }
}
